from sqlalchemy import Column, DateTime, Integer, String, BigInteger, CHAR
from sqlalchemy.sql import func
from base import Base
from sqlalchemy.orm import relationship

class SysDept(Base):
    __tablename__ = 'sys_depts'

    id = Column(BigInteger, primary_key=True, autoincrement=True, comment='部门id')
    parent_id = Column(BigInteger, default=0, comment='父部门id')
    ancestors = Column(String(50), default='', comment='祖级列表')
    dept_name = Column(String(30), default='', comment='部门名称')
    order_num = Column(Integer, default=0, comment='显示顺序')
    leader = Column(String(20), nullable=True, comment='负责人')
    phone = Column(String(11), nullable=True, comment='联系电话')
    email = Column(String(50), nullable=True, comment='邮箱')
    status = Column(CHAR(1), default='0', comment='部门状态（0正常 1停用）')
    del_flag = Column(CHAR(1), default='0', comment='删除标志（0代表存在 2代表删除）')
    create_by = Column(String(64), default='', comment='创建者')
    created_at = Column(DateTime, default=func.now(), comment='创建时间')
    update_by = Column(String(64), default='', comment='更新者')
    updated_at = Column(DateTime, onupdate=func.now(), comment='更新时间')
    deleted_at = Column(DateTime, nullable=True, comment='删除时间')

    roles = relationship('SysRole', secondary='sys_role_depts', back_populates='depts')


    def __repr__(self):
        return f"<SysDept(dept_id={self.id}, dept_name={self.dept_name})>"
